package gitobject;

import java.io.*;
import java.util.ArrayList;
import java.util.Objects;

import fileoperation.FileRead;
import fileoperation.FileReader;
import sha1.SHA1;
import repository.Repository;
import zlib.ZLibUtils;

public class Commit extends GitObject{
    protected String tree; 		// the sha1 value of present committed tree
    protected String parent; 	// the sha1 value of the parent commit
    protected String author; 	// the author's name and timestamp
    protected String committer; // the committers' information
    protected String message; 	// the commit message
    protected String branch;    // the current branch of this commit.
    // 节点提交时只会指向一个分支，但新建分支时可能会有许多个branch指向同一个commit

//FileReader读取顺序为tree、parent、author、commit、message


    public String getParent(){return parent;}
    public String getTree(){return tree;}
    public String getAuthor(){return author;}
    public String getCommitter(){return committer;}
    public String getMessage(){return message;}
    public String getBranch(){return branch;}

    public Commit(){}
    /**
     * Construct a commit directly from a file.
     * @param
     * author, committer, message参数在git commit命令里创建
     * @throws Exception
     */
    //FileReader读取顺序为tree、parent、author、commit、message
    public Commit( File index, String author, String committer, String message) throws Exception {
        getLastCommit();
    //    new Tree(index);//index以tree方式存入objects
        this.fmt = "commit"; 	//type of object
        this.tree = SHA1.getHash(FileReader.readFileValue(index));
    //    this.parent = getLastCommit() == null ? "" : getLastCommit();
        //null means there is no parent commit.
        this.author = author;
        this.committer = committer;
        this.message = message;

        /*Content of this commit, like this:
         *tree bd31831c26409eac7a79609592919e9dcd1a76f2
         *parent d62cf8ef977082319d8d8a0cf5150dfa1573c2b7
         *author xxx  1502331401 +0800
         *committer xxx  1502331401 +0800
         *修复增量bug
         * */
        this.value = "tree " + this.tree + "\nparent " + this.parent+ "\nauthor " + this.author + "\ncommitter " + this.committer + "\n" + this.message;

        key = genKey();
        writeObject();

        /*
         *commit写入branch
         */
        File branch  = new File(this.branch);
        String branchContent = this.value + "\n" + FileReader.deserializeFileValue(branch);
        new Branch(branch.getName(),branchContent);
    }
    
    /**
     * Construct a commit from a built tree.
     * @param
     * author, committer, message参数在git commit命令里创建
     * @throws Exception
     */
    public Commit(Tree t, String author, String committer, String message) throws Exception {
        getLastCommit();;
        this.fmt = "commit"; 	//type of object
        this.tree = t.getKey(); 
 //       this.parent = getLastCommit() == null ? "" : getLastCommit(); //null means there is no parent commit.
        this.author = author;
        this.committer = committer;
        this.message = message;

        /*Content of this commit, like this:
         *tree bd31831c26409eac7a79609592919e9dcd1a76f2
         *parent d62cf8ef977082319d8d8a0cf5150dfa1573c2b7
         *author xxx  1502331401 +0800
         *committer xxx  1502331401 +0800
         *修复增量bug
         * */
        this.value = "tree " + this.tree + "\nparent " + this.parent+ "\nauthor " + this.author + "\ncommitter " + this.committer + "\n" + this.message;
      //  this.path = Repository.getGitDir() + File.separator + "objects" + File.separator + "commits" + File.separator;
        this.key = genKey();
        writeObject();

        /*
         *commit写入branch
         */
        File branch  = new File(this.branch);
          String branchContent = this.value + "\n" + FileReader.deserializeFileValue(branch);
         new Branch(branch.getName(),branchContent);
    }
    
    /**
     * Deserialize the commit object from its key(commitId)
     * @param commitId
     * @throws IOException
     */
    public Commit(String commitId) throws IOException {


    }
    /**
     * Generate the hash value of this commit.
     * @return key
     * */
    public String genKey() throws Exception {
        key = SHA1.getHash(value);
        return key;
    }




    /**
     * Get the parent commit from the HEAD file.
     * 获取this.parent 和this.branch
     * @return
     * @throws IOException
     */
    //获取上次提交节点
    //可以调用FileReader.readParentFromCommit
    public  void getLastCommit() throws IOException {
        File HEAD = new File(Repository.getGitDir() + File.separator + "HEAD");
        //头指针路径为仓库地址+HEAD
        String headValue = FileRead.readBranchFromHead(HEAD);
        if(!headValue.equals(" ")){
            String path = headValue.replace("\n", "");
            //路径回溯，头结点value为"refs/heads/" + branch
            //headValue为：refs/heads/branch
            this.branch = Repository.getGitDir() + File.separator + path;
            //分支为branch所在路径
            File branchFile = new File(this.branch);
            //头结点下首元节点路径
            if (branchFile.isFile()) {
             //   return getValue(branchFile);
                this.parent = FileRead.readCommitFromBranch(branchFile);
                //返回反序列化的文件
            } else {
                this.parent = null;
            }
        }
        else {
            this.branch =  null;
        }
    }
}

